<?php
/**
 * Main controller, will handle all AJAX calls from murillodigital and perform
 * all view-model intermediation
 */
session_start();
switch ($_POST["controller_action"]) {
    case "submit_contact_form":
        $name_contents = explode(" ", $_POST["full_name"], 2);
        $description_contents = "Time Zone: {$_POST["time_zone"]} | Contact Method: {$_POST["contact_method"]}\nProject Details:\n{$_POST["project_details"]}"; 
        $lead_values = array(
            'lead_source'=> 'Web Site',
            'lead_source_description' => 'murillodigital.com',
            'lead_status' => 'New',
            'first_name' => $name_contents[0],
            'last_name' => $name_contents[1],
            'phone_work' => ($_POST["phone_work"] != "") ? $_POST["phone_work"] : "000",
            'email1' => $_POST["email1"],
            'description' => $description_contents);
        submitContactForm($lead_values);
    break;
    case "get_lead_details":
        $lead_id = $_POST["lead_id"];
        getLeadDetails($lead_id);
    break;
        
}
function submitContactForm($lead_values) {
    require_once('../models/sugarCRM.php');
    require_once('../../../config/sugarcrm_credentials.php');
    $sugar = new SugarCRMWebServices($username, $password);
    $sugar->login();
    $lead_insert_result = $sugar->createLead($lead_values);
    if ($lead_insert_result) {
        echo json_encode($lead_insert_result);
    } else {
        $error_present["error_present"] = "1";
        echo json_encode($error_present);
    }
}
function getLeadDetails($lead_id) {
    require_once('../models/sugarCRM.php');
    require_once('../../../config/sugarcrm_credentials.php');
    $sugar = new SugarCRMWebServices($username, $password);
    $sugar->login();
    $lead_details = $sugar->getLeadDetails($lead_id);
    if ($lead_details) {
        echo json_encode($lead_details);
    } else {
        $error_present["error_present"] = "1";
        echo json_encode($error_present);
    }
}
?>
